package com.idea.cery.springboot1.controller;

import com.idea.cery.springboot1.base.exception.BizException;
import com.idea.cery.springboot1.base.exception.CommonEnum;
import com.idea.cery.springboot1.base.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:cery
 * @Version 1.0
 * @Date:2020/3/19
 */
@Api(description = "全局异常测试")
@RestController
@RequestMapping("ex")
public class ExceptionController {
    @Autowired
    BizException bizException;
    @Autowired
    ResultBody   resultBody;

    @ApiOperation(value = "空异常报错", notes = "根据输入的参数判断是否有空异常", httpMethod = "GET")
    @RequestMapping(value = "/isNull")
    public ResultBody isNull(String str) {

        if (str == null || str.trim().isEmpty()) {
            throw new BizException("-1", "str为空！");
        }

        resultBody.setCode(CommonEnum.SUCCESS.getResultCode());
        resultBody.setMessage(CommonEnum.SUCCESS.getResultMsg());
        resultBody.setResult("true");

        return resultBody;
    }

    @RequestMapping(value = "/isInt", method = RequestMethod.GET)
    public boolean intEx() {

        Integer.parseInt("abc123");

        return true;
    }

    @RequestMapping(value = "/isTest", method = RequestMethod.GET)
    public boolean isTest() {
        int a = 1;
        if (a == 1) {
            try {
                Integer.parseInt("abc123");
            } catch (Exception e) {
                //注解导入异常类
                bizException.setErrorCode("-2");
                bizException.setErrorMsg("数据类型不符");
                throw bizException;
            }
        }
        return true;
    }

    @RequestMapping(value = "/isBE", method = RequestMethod.GET)
    public boolean isBaseError() {
        int a = 1;
        if (a == 1) {
            try {
                Integer.parseInt("abc123");
            } catch (Exception e) {
                //注解导入异常类
                bizException.setErrorCode(CommonEnum.BODY_NOT_MATCH.getResultCode());
                bizException.setErrorMsg(CommonEnum.BODY_NOT_MATCH.getResultMsg());
                throw bizException;
            }
        }
        return true;
    }
}
